package cn.skyquant.quant4j.sdk.util.string;

import cn.skyquant.quant4j.sdk.util.maths.MathsUtils;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.StringWriter;
import java.util.Collection;
import java.util.Map;

/**
 * Created by harley-dog on 2016/11/26.
 */
public final class StringUtils {
    public static final char[] a = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
            , 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
            , 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'Z', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    public static final char[] b = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

    public static final boolean isEmpty(String str) {
        return str == null || str.trim().length() == 0;
    }

    public static final String genString(int length) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int index = MathsUtils.getRandom(a.length);
            sb.append(a[index]);
        }
        return sb.toString();
    }

    public static final String genNumber(int length) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int index = MathsUtils.getRandom(b.length);
            sb.append(b[index]);
        }
        return sb.toString();
    }

    public static final String join(String[] args, String pre, String suf) {
        StringBuffer sb = new StringBuffer(pre);
        for (int i = 0; args != null && i < args.length; i++) {
            sb.append(args[i]).append(',');
        }
        if (sb.charAt(sb.length() - 1) == ',') {
            sb.deleteCharAt(sb.length() - 1);
        }
        sb.append(suf);
        return sb.toString();
    }


    public static final String join(Collection<String> args, String pre, String suf) {
        StringBuffer sb = new StringBuffer(pre);
        int i = 0;
        for (String str : args) {
            sb.append(str).append(',');
        }
        if (sb.charAt(sb.length() - 1) == ',') {
            sb.deleteCharAt(sb.length() - 1);
        }
        sb.append(suf);
        return sb.toString();
    }

    /**
     * 替换字符串中的变量
     *
     * @param s      hello,${name}
     * @param params name=world
     * @return hello, world
     */
    public static final String replace(String s, Map<String, String> params) throws Exception {
        StringTemplateLoader stringLoader = new StringTemplateLoader();
        String template = "content";
        stringLoader.putTemplate(template, s);
        Configuration cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        cfg.setTemplateLoader(stringLoader);
        try {
            Template templateCon = cfg.getTemplate(template);
            StringWriter writer = new StringWriter();
            templateCon.process(params, writer);
            return writer.toString();
        } catch (Exception e) {
            throw e;
        }
    }

    public static final int[] stringToIntArray(String str) {
        return stringToIntArray(str, ",");
    }

    public static final int[] stringToIntArray(String str, String split) {
        String[] strarr = str.split(split);
        int[] r = new int[strarr.length];
        for (int i = 0; i < strarr.length; i++) {
            r[i] = Integer.parseInt(strarr[i].trim());
        }
        return r;
    }

    public static final double[] stringToDoubleArray(String str) {
        return stringToDoubleArray(str, ",");
    }

    public static final double[] stringToDoubleArray(String str, String split) {
        String[] strarr = str.split(split);
        double[] r = new double[strarr.length];
        for (int i = 0; i < strarr.length; i++) {
            r[i] = Double.parseDouble(strarr[i].trim());
        }
        return r;
    }


    public static final String doubleArrayToString(double[] ds) {
        return doubleArrayToString(ds, ",", "", "");
    }

    public static final String intArrayToString(int[] ds) {
        return intArrayToString(ds, ",", "", "");
    }

    public static final String doubleArrayToString(double[] ds, String split, String pre, String sub) {
        StringBuilder sb = new StringBuilder(pre);
        for (double s : ds) {
            sb.append(String.format("%.2f", s)).append(split);
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append(sub);
        return sb.toString();
    }


    public static final String intArrayToString(int[] ds, String split, String pre, String sub) {
        StringBuilder sb = new StringBuilder(pre);
        for (int s : ds) {
            sb.append(s).append(split);
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append(sub);
        return sb.toString();
    }
}
